import { computed } from 'vue'

export function usePriceCalculation() {
  const calculatePrice = ({
    dm, num, mj, 
    materialTotalPrice, materialTotalWeight,
    yinPrice, fuhePrice, chen,
    xiPrice, laPrice, qiPrcie,
    xis, ke, zhidaiPrice, tejai,
    kaijiPrice, fapiao, fujia,
    lake, qiKe, xiKe
  }) => {
    // 1. 计算单只重量
    const singleWeight = 
      Number(dm) * materialTotalWeight +
      Number(lake) +
      Number(qiKe) +
      Number(xiKe)
    
    const dj = truncateDecimals(singleWeight, 6)
    const zl = Number(dj) * Number(num)

    // 2. 计算材料成本
    const materialCost = 
      materialTotalPrice * Number(dm) +
      Number(yinPrice) +
      Number(fuhePrice) * Number(chen)

    // 3. 计算附加成本
    const additionalCost = 
      Number(xiPrice) + 
      Number(laPrice) + 
      Number(qiPrcie)

    // 4. 计算系数成本
    const coefficientCost = materialCost * (Number(xis) + Number(ke))

    // 5. 计算总成本
    let totalCost = coefficientCost + 
      Number(zhidaiPrice) + 
      additionalCost + 
      Number(tejai)

    if (Number(mj) <= 4000) {
      totalCost += Number(kaijiPrice)
    }

    // 6. 最终价格
    const djia = truncateDecimals(totalCost * Number(fapiao), 4)
    const totalPrice = truncateDecimals(
      Number(djia) * Number(num) + Number(fujia), 
      4
    )

    // 计算厚度
    const dz = (
      Number(thick.a) +
      Number(thick.b) +
      Number(thick.c) +
      Number(thick.d) +
      Number(thick.e))
    ) / 1000

    const mj = truncateDecimals(Number(dm) * Number(num), 4)

    return {
      dj,
      zl,
      djia,
      totalPrice,
      dz,
      mj
    }
  }

  return {
    calculatePrice
  }
}